package com.jason.kotlin.gank.ui

import android.text.TextUtils
import android.widget.Button
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.model.RollCommonBean
import com.jason.kotlin.gank.ui.adapter.RollCommonQueryAdapter
import com.jason.kotlin.gank.viewmodel.RollMoreViewModel
import com.jason.library.common.base.BaseStateActivity
import com.jason.library.common.ext.observe
import com.jason.library.common.util.KeyboardUtils
import com.jason.library.widget.edittext.MClearEditText
import com.jason.library.widget.snackbar.MySnackbar

/**
 * @description: RollDictionaryActivity
 *
 * @author ChenBaoyang
 * @date 4/25/21 10:58
 */
class RollDictionaryActivity : BaseStateActivity<RollMoreViewModel>() {

    private lateinit var queryBtn: Button
    private lateinit var editText: MClearEditText
    private lateinit var recyclerView: RecyclerView

    private lateinit var queryAdapter: RollCommonQueryAdapter

    override fun bindLiveData() {
        observe(viewModel!!.dataDictionary) {
            val datas = mutableListOf<RollCommonBean>()
            datas.add(RollCommonBean("原内容:", it.word))
            datas.add(RollCommonBean("繁体:", it.traditional))
            datas.add(RollCommonBean("拼音:", it.pinyin))
            datas.add(RollCommonBean("偏旁部首:", it.radicals))
            datas.add(RollCommonBean("汉字释义:", it.explanation))
            datas.add(RollCommonBean("汉字笔画数:", "${it.strokes}"))
            queryAdapter.setNewData(datas)
        }
    }

    override fun getPageTarget() = recyclerView

    override fun initView() {
        title = "新华字典"
        queryBtn = findViewById(R.id.btn_query)
        editText = findViewById(R.id.editText)
        recyclerView = findViewById(R.id.recyclerView)

        queryAdapter = RollCommonQueryAdapter(R.layout.roll_item_common_query)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = queryAdapter
        recyclerView.itemAnimator = DefaultItemAnimator()

        queryBtn.setOnClickListener {
            KeyboardUtils.hideInputKeyboard(this)

            val word: String = editText.getText().toString()

            if (TextUtils.isEmpty(word)) {
                MySnackbar.makeSnackBarRed(toolbar, "查询内容不能为空")
                return@setOnClickListener
            }

            viewModel!!.convertDictionary(word)
        }
    }

    override fun reload() {
    }

    override fun getLayoutId() = R.layout.roll_activity_dictionary

    override fun onDataInit() {
    }
}